package com.llg.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llg.mapper.LedMatchReceiveMapper;
import com.llg.mapper.LedSpecMapper;
import com.llg.model.SpecModel;
import com.llg.model.code.CommonCode;
import com.llg.model.code.PageCode;
import com.llg.model.response.*;
import com.llg.pojo.LedMatchReceive;
import com.llg.pojo.LedSpec;
import com.llg.util.Entity2Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class LedSpecService {

    @Autowired
    private LedSpecMapper specMapper;
    @Autowired
    private LedMatchReceiveMapper ledMatchReceiveMapper;

    //根据品牌id查询所有型号
    public PageResult<SpecModel> findByBrandId(Integer bid) {
        Example example = new Example(LedSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bid", bid);
        List<LedSpec> specs = specMapper.selectByExample(example);
        List<SpecModel> specModels = new ArrayList<>();
        for (LedSpec spec : specs) {
            specModels.add(Entity2Model.spec2Model(spec));
        }
        return new PageResult<>(CommonCode.SUCCESS, 1, 1, specModels);
    }

    //搜索并分页查询
    public PageResult<SpecModel> findList(int currentPage, int pageSize, LedSpec ledSpec) {
        //判断参数是否合法
        if (currentPage <= 0) return new PageResult<>(PageCode.PAGE_ERR1, 0, 0, null);
        if (pageSize <= 0) return new PageResult<>(PageCode.SIZE_ERR, 0, 0, null);
        PageHelper.startPage(currentPage, pageSize);
        Example example = new Example(LedSpec.class);
        Example.Criteria criteria = example.createCriteria();
        if (ledSpec != null) {
            if (ledSpec.getName() != null && !ledSpec.getName().equals("")) {
                criteria.andLike("name", "%" + ledSpec.getName() + "%");
            }
        }
        List<LedSpec> ledSpecs = specMapper.selectByExample(example);
        PageInfo<LedSpec> page = new PageInfo<>(ledSpecs);
        List<SpecModel> list = new ArrayList<>();
        for (LedSpec spec : page.getList()) {
            list.add(Entity2Model.spec2Model(spec));
        }
        return new PageResult<>(CommonCode.SUCCESS, (int) page.getTotal(), page.getPageNum(), list);
    }


    //根据id查询
    public DataResult<SpecModel> findById(Integer id) {
        LedSpec spec = specMapper.selectByPrimaryKey(id);
        return new DataResult<>(CommonCode.SUCCESS, Entity2Model.spec2Model(spec));
    }

    //通过接收卡id查找所有未与该接收卡建立对应关系的led型号
    public PageResult<SpecModel> findByRid(Integer rid){
        List<LedSpec> specs = specMapper.findByRid(rid);
        List<SpecModel> models = new ArrayList<>();
        for (LedSpec spec : specs) {
            models.add(Entity2Model.spec2Model(spec));
        }
        return new PageResult<>(CommonCode.SUCCESS, models.size(), 1, models);
    }

    //新增型号
    @Transactional
    public DataResult<SpecModel> save(SpecModel specModel) {
        //保存led信息
        LedSpec spec = Entity2Model.model2Spec(specModel);
        specMapper.insert(spec);
        //保存所有对应的接收卡信息
        if (specModel.getReceives() != null && specModel.getReceives().size() > 0) {
            for (LedMatchReceive receive : specModel.getReceives()) {
                receive.setLid(specModel.getId());
                ledMatchReceiveMapper.insertSelective(receive);
            }
        }
        return new DataResult<>(CommonCode.SUCCESS, Entity2Model.spec2Model(spec));
    }


    //修改型号
    public ResponseResult update(SpecModel specModel) {
        LedSpec spec = Entity2Model.model2Spec(specModel);
        specMapper.updateByPrimaryKeySelective(spec);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //删除型号
    public ResponseResult delete(Integer id) {
        specMapper.deleteByPrimaryKey(id);
        return new ResponseResult(CommonCode.SUCCESS);
    }
}
